﻿/*
   Copyright 2012 University of Southampton

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace uk.ac.soton.ses.layouts
{
    public partial class CTDataViewer : LayoutsPageBase
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.Page.AddHDCStylesheet();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                CtDataViewerUserControl ctdataviewer_control = (CtDataViewerUserControl)this.Control_CTDataViewer;
                ctdataviewer_control.DataFileID = this.GetDataFileIDFromPageRequest();
            }
            catch (FormatException)
            {
                this.Control_CTDataViewer.Visible = false;
                HDCSharedWebFunctions.AddErrorLabel("Please pass in DataFileID parameter in page request by adding '?DataFileID=number' to URL", this.PlaceHolder_Messages);
            }
        }

        private int GetDataFileIDFromPageRequest()
        {
            //We will try to look one up the data file ID from the page request
            string datafileid_str = Page.Request["DataFileID"];
            int parseddatafileid;
            if (!string.IsNullOrEmpty(datafileid_str) && int.TryParse(datafileid_str, out parseddatafileid))
            {
                return parseddatafileid;
            }
            else
            {
                throw new FormatException("An integer is required for DataFileID");
            }
        }
    }
}
